package com.example.administrator.shzs.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.db.GoodsDataBaseInterface;
import com.example.administrator.shzs.db.OperateGoodsDataBase;
import com.example.administrator.shzs.entity.SortContentData.FoodlistBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.xutils.x;

import java.util.List;

/**
 * 商家店铺分类的填充内容Adapter
 */

public class ShopContentAdapter extends XRecyclerView.Adapter<ShopContentAdapter.MyViewHolder> {

    private Context context;
    private List<FoodlistBean> lists;
    private List<FoodlistBean.StylelistBean> stylelistBeen;

    private MyViewHolder holder;

    /** 数据库操作接口 */
    private GoodsDataBaseInterface mGoodsDataBaseInterface = null;
    //商家Id
    private String shopId;


    public ShopContentAdapter(Context context, List<FoodlistBean> lists,String shopId) {
        this.context = context;
        this.lists = lists;
        mGoodsDataBaseInterface = OperateGoodsDataBase.getInstance();
        this.shopId=shopId;
    }

    @Override
    public ShopContentAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建View对象
        View view = View.inflate(context, R.layout.shop_content_item, null);

        holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ShopContentAdapter.MyViewHolder holder, final int position) {

        //商品图片
        x.image().bind(holder.ivIcon, lists.get(position).getIcon());


        //商品名称
        final String name=lists.get(position).getName();
        holder.tvName.setText(name);

        //商品唯一Id
        final String foodId=lists.get(position).getFoodID();

        //获取每一个Item的stylelistBeen
        stylelistBeen=lists.get(position).getStylelist();

        //从stylelistBeen中获取商品价格
        double price=stylelistBeen.get(0).getPrice();
        holder.tvStartingPrice.setText(price+"");


        /** 先获取单个存储的商品数量 */
        if(mGoodsDataBaseInterface.getGoodsNumber(context,Integer.parseInt(shopId.trim()),Integer.parseInt(foodId)) ==0 ){

            holder.tvNumber.setText("");
            holder.tvNumber.setVisibility(View.GONE);
            holder.ivJian.setVisibility(View.GONE);

        }else {
            //先获取单个商品数量
            holder.tvNumber.setText(""+mGoodsDataBaseInterface.getGoodsNumber(context,Integer.parseInt(shopId.trim()),Integer.parseInt(foodId)));
            holder.tvNumber.setVisibility(View.VISIBLE);
            holder.ivJian.setVisibility(View.VISIBLE);
        }


        //加号、减号按钮监听
        setOnListtener(holder);


    }



    /**
     * 监听
     * @param holder
     */
    private void setOnListtener(final MyViewHolder holder) {

        if(mOnItemClickListener != null){

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder);
                }
            });

        }

        if (mOnItemClickListener != null) {

            //给加号按钮注册点击事件
            holder.ivAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onAddClick(holder);
                }
            });

        }


        if(mOnItemClickListener != null){
            holder.ivJian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onJianClick(holder);

                }
            });
        }


    }


    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder{

        public ImageView ivIcon;//商品图
        public TextView tvName;//商品名称
        public TextView tvMonthlySales;//月售
        public TextView tvPraise;//点赞量
        public TextView tvStartingPrice;//起价
        public ImageView ivAdd;//加购物车
        public ImageView ivJian;
        public TextView tvNumber;

        public MyViewHolder(View itemView) {
            super(itemView);

            ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon_shop_content_item);
            tvName = (TextView) itemView.findViewById(R.id.tv_name_shop_content_item);
            tvStartingPrice= (TextView) itemView.findViewById(R.id.tv_starting_price_shop_content_item);

            tvNumber= (TextView) itemView.findViewById(R.id.tv_number_shop_content_item);
            ivAdd = (ImageView) itemView.findViewById(R.id.iv_add_shop_contenr_item);
            ivJian= (ImageView) itemView.findViewById(R.id.iv_jian_shop_contenr_item);
        }


    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(MyViewHolder holder );
        void onAddClick(MyViewHolder holder);
        void onJianClick(MyViewHolder holder);
    }


}
